package cn.ekuma.data.ui.android;

import java.util.List;

import cn.ekuma.android.Animation.ExpandCollapseAnimation;
import cn.ekuma.data.dao.I_DataLogic;
import cn.ekuma.data.dao.bean.I_ViewBean;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public abstract class AbstractListFinderActivate<T extends I_ViewBean> extends AbstractFinderActivate<T> implements View.OnTouchListener{
	
	protected BaseBeanListSelecter<T> main;
	protected AbstractViewBeanListAdapter listAdapter;
    ListView listView;
	
	public AbstractListFinderActivate(I_DataLogic dataLogic) {
		super(dataLogic);

	}

	@Override
	public View createContentPanel() {
        touchSlop = ViewConfiguration.get(this.getBaseContext()).getScaledTouchSlop();
		main=new BaseBeanListSelecter<T>(this);
		listAdapter=getListAdapter();
		main.setAbstractBaseBeanAdapter(listAdapter);
		main.setOnBaseBeanSelectedListener(this);
        listView=main.getListview();
        listView.setOnTouchListener(this);
		return main;
	}

	public abstract AbstractViewBeanListAdapter getListAdapter();
	
	
	protected Handler mHandler = new Handler() {  
        public void handleMessage (Message msg) {//此方法在ui线程运行   
            switch(msg.what) {  
            case MSG_SUCCESS:  
            	listAdapter.clear();
				for(T info:(List<T>)msg.obj)
				listAdapter.add(info);
                break;  
            case MSG_FAILURE:  
                Toast.makeText(getApplication(), "执行出错了", Toast.LENGTH_LONG).show();  
                break;
            }  
        }  
    };


    /**
     * 进行一些关键性的初始化操作，比如：将下拉头向上偏移进行隐藏，给ListView注册touch事件。
     *
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            hideHeaderHeight =-filterPanel.getHeight();
            headerLayoutParams = (ViewGroup.MarginLayoutParams) filterPanel.getLayoutParams();
            headerLayoutParams.topMargin = hideHeaderHeight;
        }
    }*/

    /**
     * 当ListView被触摸时调用，其中处理了各种下拉刷新的具体逻辑。
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        System.out.println("touch at :"+event.getRawY());
        setIsAbleToPull(event);
        if (ableToPull) {
            System.out.println("able to pull");
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    yDown = event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_UP:
                    float yMove = event.getRawY();
                    int distance = (int) (yMove - yDown);
                    // 如果手指是下滑状态，并且下拉头是完全隐藏的，就屏蔽下拉事件

                    if (distance <= 0 &&distance < touchSlop*2) {
                        return false;
                    }

                         // 通过偏移下拉头的topMargin值，来实现下拉效果
                        //headerLayoutParams.topMargin = (distance / 2) + hideHeaderHeight;
                        //filterPanel.setLayoutParams(headerLayoutParams);
                        if(titlePanel.getVisibility()==View.GONE)
                            //titlePanel.setVisibility(View.VISIBLE);
                            titlePanel.startAnimation(new ExpandCollapseAnimation(titlePanel,ExpandCollapseAnimation.EXPAND));
                        else
                            //titlePanel.setVisibility(View.GONE);
                            titlePanel.startAnimation(new ExpandCollapseAnimation(titlePanel,ExpandCollapseAnimation.COLLAPSE));
                    return true;

            }
        }
        return false;
    }

    /**
     * 根据当前ListView的滚动状态来设定 {@link #ableToPull}
     * 的值，每次都需要在onTouch中第一个执行，这样可以判断出当前应该是滚动ListView，还是应该进行下拉。
     *
     * @param event
     */
    private void setIsAbleToPull(MotionEvent event) {
        View firstChild = listView.getChildAt(0);
        if (firstChild != null) {
            int firstVisiblePos = listView.getFirstVisiblePosition();
            if (firstVisiblePos == 0 && firstChild.getTop() == 0) {
                if (!ableToPull) {
                    yDown = event.getRawY();
                }
                // 如果首个元素的上边缘，距离父布局值为0，就说明ListView滚动到了最顶部，此时应该允许下拉刷新
                ableToPull = true;
            }else
                ableToPull = false;
        } else {
            // 如果ListView中没有元素，不应该允许下拉刷新
            ableToPull = true;
        }
    }


    /**
     * 当前是否可以下拉，只有ListView滚动到头的时候才允许下拉
     */
    private boolean ableToPull;

    /**
     * 手指按下时的屏幕纵坐标
     */
    private float yDown;

    /**
     * 在被判定为滚动之前用户手指可以移动的最大值。
     */
    private int touchSlop;


    /**
     * 下拉头的布局参数
     */
    private ViewGroup.MarginLayoutParams headerLayoutParams;

    /**
     * 下拉头的高度
     */
    private int hideHeaderHeight;

}
